import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
    meta: { title: '后台登录' }
  },

  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 404 page must be placed at the end !!!
  {
    path: '/message-aisle',
    component: Layout,
    name: '通道设置',
    redirect: '/message-aisle/list',
    meta: { title: '通道设置', icon: 'el-icon-guide' },
    children: [
      {
        path: '/message-aisle/list',
        component: () => import('@/views/message-aisle/index'),
        name: '通道列表',
        meta: { title: '通道列表', icon: 'edit' }
      },
      {
        path: '/message-aisle/add',
        name: '添加通道',
        component: () => import('@/views/message-aisle/add'),
        meta: { title: '添加通道', icon: 'el-icon-folder-add' }
      },
      {
        path: '/message-aisle/edit/:id',
        name: 'editMessage',
        component: () => import('@/views/message-aisle/edit'),
        meta: { title: '修改通道' },
        hidden: true
      }
    ]
  },
  {
    path: '/user-pricing',
    component: Layout,
    name: 'userPrice',
    redirect: '/user-pricing/list',
    meta: { title: '资源价格', icon: 'el-icon-guide' },
    children: [
      {
        path: '/user-pricing/list',
        component: () => import('@/views/user-pricing/list'),
        name: '资源列表',
        meta: { title: '资源列表', icon: 'edit' }
      },
      {
        path: '/user-pricing/add',
        name: 'addUserPricing',
        component: () => import('@/views/user-pricing/add'),
        meta: { title: '添加资源', icon: 'el-icon-folder-add' }
      },
      {
        path: '/user-pricing/edit/:id',
        name: 'editUserPricing',
        component: () => import('@/views/user-pricing/edit'),
        meta: { title: '修改资源' },
        hidden: true
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
